/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinker/icare">Thinker</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package cn.com.thinker.security.sdk.system.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.transaction.Transactional;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.com.thinker.security.sdk.system.entity.SystemUser;


//import com.thinker.icare.common.persistence.BaseDao;
//import com.thinker.icare.common.persistence.Parameter;
//import com.thinker.icare.modules.sys.entity.User;

/**
 * 用户DAO接口
 * @author Thinker
 * @version 2013-8-23
 */
@Repository
public class SystemUserDao {
	
	
	
	/**
	 * SessionFactory
	 */
	@Autowired
	private SessionFactory sessionFactory;

	/**
	 * 获取 Session
	 */
	public Session getSession(){  
	  return sessionFactory.getCurrentSession();
	}

	/**
	 * 强制与数据库同步
	 */
	public void flush(){
		getSession().flush();
	}

	/**
	 * 清除缓存数据
	 */
	public void clear(){ 
		getSession().clear();
	}
	
	// -------------- QL Query --------------

	
	/**
	 * 获取实体
	 * @param qlString
	 * @return
	 */
	public SystemUser getByHql(String qlString){
		return getByHql(qlString, null);
	}
	
	/**
	 * 获取实体
	 * @param qlString
	 * @param parameter
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public SystemUser getByHql(String qlString, Map<String,Object> parameter){
		Query query = createQuery(qlString, parameter);
		return (SystemUser)query.uniqueResult();
	}
	
	/**
	 * 构造类，例：new Parameter(id, parentIds)
	 * @param values 参数值
	 */
	public Map<String,Object> getParameter(Object... values) {
		Map<String,Object> map = null;
		if (values != null){
			map = new HashMap<String, Object>();
			for (int i=0; i<values.length; i++){
				map.put("p"+(i+1), values[i]);
			}
		}
		return map;
	}
	
	public SystemUser findByLoginName(String loginName){
		
		return getByHql("from SystemUser where loginName = :p1 and delFlag = :p2", getParameter(loginName, "0"));
		
//		return getByHql("from User where loginName = :p1 and delFlag = :p2", getParameter(loginName, "0"));
	}

	
	/**
	 * 创建 QL 查询对象
	 * @param qlString
	 * @param parameter
	 * @return
	 */
	public Query createQuery(String qlString, Map<String,Object> parameter){
		Query query = getSession().createQuery(qlString);
		setParameter(query, parameter);
		return query;
	}
	
	
	/**
	 * 设置查询参数
	 * @param query
	 * @param parameter
	 */
	private void setParameter(Query query, Map<String,Object> parameter){
		if (parameter != null) {
            Set<String> keySet = parameter.keySet();
            for (String string : keySet) {
                Object value = parameter.get(string);
                //这里考虑传入的参数是什么类型，不同类型使用的方法不同  
                if(value instanceof Collection<?>){
                    query.setParameterList(string, (Collection<?>)value);
                }else if(value instanceof Object[]){
                    query.setParameterList(string, (Object[])value);
                }else{
                    query.setParameter(string, value);
                }
            }
        }
	}
	
	
}
